<?php
declare (strict_types = 1);

namespace app\api\controller;

use think\exception\ValidateException;

use app\common\model\{User};
use app\common\validate\UserValidate;

class LoginController extends BaseController
{

    private $userModel;

    public function __construct(User $userModel)
    {
        parent::initialize();
        $this->userModel = $userModel;
    }

    /** 登陆 */
    public function login()
    {
        $data = input('');

        try{
            validate(UserValidate::class)->scene('login')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        /** 查询用户信息 */
        $user_info = $this->userModel->getFind(['username' => $data['username']], 'id, password');

        if($user_info['code'] != 'ok'){
            $this->error_back('账号不存在');
        }

        if(setPass($data['password']) != $user_info['data']['password']){
            $this->error_back('密码错误');
        }

        $this->success_back('登陆成功', $user_info['data']['id']);
    }
}
